package com.travel.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;

import com.travel.entities.Train;
import com.travel.service.TrainService;
import com.travel.util.Constants;

/**
 * 查询列车信息接口控制器
 *
 * @author SAGITTAR
 */
public class TrainServlet extends HttpServlet {

	private static final long serialVersionUID = 9174262292976804468L;
    private static final Logger LOGGER = LogManager.getLogger(TrainServlet.class);

	private static final TrainService TRAIN_SERVICE = new TrainService();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String startStation = req.getParameter("startStation");
		String endStation = req.getParameter("endStation");
		String startDate = req.getParameter("startDate");
        LOGGER.info("传入查询参数为:起点:{}, 终点:{}, 日期:{}", startStation, endStation, startDate);
        // List<Train> trainList = TRAIN_SERVICE.getTrainListByStation(startStation, endStation, startDate);
        List<Train> trainList = TRAIN_SERVICE.getTrainListByStation12306(startStation, endStation, startDate);
		String result;
		if (trainList.isEmpty()) {
			result = "没有查询到车次信息，请确认发站到站信息";
		} else {
			result = new JSONArray(trainList).toString();
		}
		resp.setCharacterEncoding(Constants.UTF_8);
        resp.getWriter().write(result);
        // resp.getOutputStream().write(result.getBytes());
	}
}
